<?php

namespace App\Admin\Controllers;

use App\Models\AdminUser;
use Encore\Admin\Auth\Database\Administrator;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\DB;
use function Clue\StreamFilter\fun;

class AdminUserController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '环信账号创建';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new AdminUser());
        $grid->disableExport();//禁止导出
        $grid->disableRowSelector();
        $grid->disableCreateButton();//禁用创建按钮
        $grid->actions(function ($actions) {
            $actions->disableDelete(); //禁用删除按钮
            $actions->disableEdit();//禁止行级编辑路由
            $actions->append("<a class='btn btn-xs action-btn btn-success grid-row-pass' data-id='{$actions->getKey()}'><i class='fa fa-check' title='创建环信'>创建账号</i></a>");
        });
        $this->script = <<<EOT
        //创建环信账号
        $('.grid-row-pass').unbind('click').click(function() {
            var id = $(this).data('id');
            swal({
                title: "确认创建？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {
                    $.ajax({
                        method: 'POST',
                        dataType: 'json',
                        url: '/admin/cj/',
                        data:{
                            _token:LA.token,
                            id:id,
                        },
                        success: function (data) {
                            console.log(data);
                            $.pjax.reload('#pjax-container');
                            if(data.code){
                                swal(data.msg, '', 'success');
                            }else{
                                swal(data.msg, '', 'error');
                            }
                        }
                    });
                }
            });
        });
EOT;
        Admin::script($this->script);
        $grid->fixColumns(0, 1);//固定列可滑动列表
        $grid->column('id', __('ID'));
        $grid->column('roles', __('环信账号'))->display(function () {
            $res = DB::table('admin_role_users')->where('user_id', '=', $this->id)->select(['role_id'])->first();
            $roleid = $res->role_id ?? 0;
            if ($roleid == 1) {
                return 'adminag6001' . $this->id;
            } elseif ($roleid == 2) {
                return 'suppliersag6001' . $this->id;
            } else {
                return '账号角色异常';
            }
        });
        $grid->column('username', __('账号'));
        $grid->column('name', __('角色'));
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $userModel = config('admin.database.users_model');
        $show = new Show($userModel::findOrFail($id));
        $show->field('id', __('Id'));
        $show->field('username', __('账号'));
        $show->field('name', __('角色'));
        return $show;
    }
    /**
     * Make a form builder.
     *
     * @return Form
     */
//    protected function form()
//    {
//        $form = new Form(new AdminUser());
//        return $form;
//    }
}
